package entity

import (
	"encoding/json"
	"time"
)

// 充值活动
type Pay struct {
	RoleID    int32  `gorm:"type:int;primary_key"`
	Date      int32  // 活动开启的日期
	PayData   []byte // proto.Marshal(pb.PayActivityResp)
	Data      []byte `gorm:"type:jsonb"` // json.Marshal(self.data)
	CreatedAt time.Time
	UpdatedAt time.Time

	data map[string]bool `sql:"-"`
}

func TableName() string {
	return "act_pays"
}

func (e *Pay) Init() {
	e.data = make(map[string]bool)
}

func (e *Pay) AfterFind() error {

	e.data = make(map[string]bool)
	return json.Unmarshal(e.Data, &e.data)
}

func (e *Pay) BeforeSave() (err error) {

	if e.data == nil {
		e.data = make(map[string]bool)
	}

	e.Data, err = json.Marshal(e.data)
	return err
}

func (e *Pay) BeforeUpdate() (err error) {

	if e.data == nil {
		e.data = make(map[string]bool)
	}

	e.Data, err = json.Marshal(e.data)
	return err
}

func (e *Pay) Get(key string) bool {
	return e.data[key]
}

func (e *Pay) Set(key string) {
	e.data[key] = true
}
